CalendarGcMaskCellTypeでは様々な視覚的な補助機能を提供しています。この章ではそれらの機能について解説します。
書式の設定
フィールド
CalendarGcMaskCellTypeでは正規表現による書式設定が可能です。書式の設定はFieldsプロパティによって行われ、フィールドとよばれる入力領域を構成する要素によって定義されます。Fieldsプロパティについては、「書式を設定する」で詳しく解説します。
CalendarGrid のデザイナ上で書式を設定するには、次の2通りのデザイン機能が用意されています。
- フィールドコレクションエディタを使用する
- キーワードから書式を生成する
フィールドコレクションエディタを使用する
CalendarGcMaskCellTypeで設定可能な3種類のフィールドを組み合わせて書式を設定する方法です。次のいずれかの方法で設定することができます。
- デザイナのプロパティウィンドウのFields プロパティから開かれる[フィールドコレクションエディタ]
このデザイン機能では、コレクションにフィールドを追加すると同時に各フィールドのテキストのフォントや色といったスタイルを設定することができます。

(図)フィールド毎に異なったスタイルを設定
リテラル文字とプロンプト文字
ShowLiteralsプロパティを使えば、入力中にリテラル文字列を表示するかどうかを指定できます。
リテラル文字とは、MaskLiteralField(リテラルフィールド)で定義された文字列をそのまま表示する文字列をいいます。
入力パターンフィールドには、プロンプト文字を設定することができます。プロンプト文字はPromptCharプロパティを使用します。プロンプト文字を使用することで、入力フィールドを明示的に表示したり入力文字数を視覚的に表すことができます。
また、InputStatusプロパティによって、すべてのフィールドに値が入力されているかどうかを確認することが可能です。このプロパティは、セルの編集時のみ有効です。
クリップボードにリテラル文字を含まない値を渡すには、ClipContentプロパティとSelectedTextプロパティを使用します。ClipContentプロパティで制御できるのは、SelectedTextプロパティの値のみです。なお、プロンプト文字列は、ClipContentプロパティの設定に関わらず常にクリップボードに渡されます。
次のサンプルコードは、ClipContentプロパティを使って、リテラル文字列を省いた文字列をクリップボードにコピーする方法を示します。
Imports GrapeCity.Win.CalendarGrid
Imports InputManCell = GrapeCity.Win.CalendarGrid.InputMan
Imports CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim GcMaskCellType As New InputManCell.CalendarGcMaskCellType()
GcMaskCellType.Fields.AddRange("郵便番号:\D{3}-\D{4}")
GcMaskCellType.ClipContent = InputManCell.ClipContent.ExcludeLiterals
Dim textBoxCellType = New CalendarTextBoxCellType()
Dim Template As New CalendarTemplate()
Template.RowCount = 3
Template.ColumnHeader.Rows(0).Cells(0).DateFormat = "{DayOfWeek}"
Template.ColumnHeader.Columns(0).Width = 120
Template.Content.Rows(0).Cells(0).DateFormat = "{MonthDay}"
Template.Content.Rows(0).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Name = "myCell1"
Template.Content.Rows(1).Cells(0).CellType = GcMaskCellType.Clone()
Template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
Template.Content.Rows(1).Cells(0).Value = "9813205"
Template.Content.Rows(2).Cells(0).CellType = textBoxCellType.Clone()
Template.Content.Rows(2).Cells(0).CellStyleName = "defaultStyle"
GcCalendarGrid1.Template = Template
AddHandler GcCalendarGrid1.EditingControlShowing, AddressOf GcCalendarGrid1_EditingControlShowing
End Sub
Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs)
If TypeOf e.Control Is CalendarGridInputMan.GcMask Then
RemoveHandler e.Control.KeyDown, AddressOf Editor_KeyDown
AddHandler e.Control.KeyDown, AddressOf Editor_KeyDown
End If
End Sub
Private Sub Editor_KeyDown(sender As Object, e As KeyEventArgs)
Dim cell As CalendarCell = GcCalendarGrid1(GcCalendarGrid1.CurrentCellPosition.Date) _
(GcCalendarGrid1.CurrentCellPosition.RowIndex, GcCalendarGrid1.CurrentCellPosition.ColumnIndex)
Dim editor As CalendarGridInputMan.GcMask = DirectCast(sender, CalendarGridInputMan.GcMask)
If e.KeyCode = Keys.F5 Then
' クリップボードにコピーします。
editor.SelectionStart = 0
editor.SelectionLength = editor.Text.Length
Clipboard.SetDataObject(editor.SelectedText)
' クリップボードのデータを取得して確認します。
Dim cbData As IDataObject = Clipboard.GetDataObject()
GcCalendarGrid1(GcCalendarGrid1.CurrentCellPosition.Date)(cell.RowIndex + 1, cell.ColumnIndex).Value =
cbData.GetData(DataFormats.Text).ToString()
End If
End Sub
using GrapeCity.Win.CalendarGrid;
using InputManCell = GrapeCity.Win.CalendarGrid.InputMan;
using CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors;
private void Form1_Load(object sender, EventArgs e)
{
var gcMaskCellType = new InputManCell.CalendarGcMaskCellType();
gcMaskCellType.Fields.AddRange("郵便番号:\\D{3}-\\D{4}");
gcMaskCellType.ClipContent = InputManCell.ClipContent.ExcludeLiterals;
var textBoxCellType = new CalendarTextBoxCellType();
var template = new CalendarTemplate();
template.RowCount = 3;
template.ColumnHeader.Rows[0].Cells[0].DateFormat = "{DayOfWeek}";
template.ColumnHeader.Columns[0].Width = 120;
template.Content.Rows[0].Cells[0].DateFormat = "{MonthDay}";
template.Content.Rows[0].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Name = "myCell1";
template.Content.Rows[1].Cells[0].CellType = gcMaskCellType.Clone();
template.Content.Rows[1].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Value = "9813205";
template.Content.Rows[2].Cells[0].CellType = textBoxCellType.Clone();
template.Content.Rows[2].Cells[0].CellStyleName = "defaultStyle";
gcCalendarGrid1.Template = template;
gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}
private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
if (e.Control is CalendarGridInputMan.GcMask)
{
// GcMaskCellの編集用コントロールが表示された場合にKeyDownイベントを設定します。
e.Control.KeyDown -= new KeyEventHandler(Editor_KeyDown);
e.Control.KeyDown += new KeyEventHandler(Editor_KeyDown);
}
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
CalendarCell cell = gcCalendarGrid1[gcCalendarGrid1.CurrentCellPosition.Date]
[gcCalendarGrid1.CurrentCellPosition.RowIndex, gcCalendarGrid1.CurrentCellPosition.ColumnIndex];
var editor = sender as CalendarGridInputMan.GcMask;
if (e.KeyCode == Keys.F5)
{
// クリップボードにコピーします。
editor.SelectionStart = 0;
editor.SelectionLength = editor.Text.Length;
Clipboard.SetDataObject(editor.SelectedText);
// クリップボードのデータを取得して確認します。
IDataObject cbData = Clipboard.GetDataObject();
gcCalendarGrid1[gcCalendarGrid1.CurrentCellPosition.Date][cell.RowIndex + 1, cell.ColumnIndex].Value =
cbData.GetData(DataFormats.Text).ToString();
}
}
編集モードの切り替え
EditModeプロパティを使って、セルがフォーカスを受け取ったときのデフォルトの編集モードを定義できます。EditModeプロパティをEditMode.Insertにすると挿入モード、EditMode.Overwriteにすると上書きモードになります。また、EditMode.FixedInsertとEditMode.FixedOverwriteでは、編集モードが固定されるので、実行中に[Ins]キーが押されても編集モードは切り替わりません。
セルの編集時では、EditModeプロパティがEditMode.InsertまたはEditMode.Overwriteに設定されている場合、編集モードが[Ins]キーまたはEditModeプロパティによって変更されたときには、GcMask.EditStatusChangedイベントが発生します。また、GcMask.Overwriteプロパティを使って編集モードを調べることができます。
次のサンプルコードは、GcMask.KeyDownイベントで[Alt]キーおよび[C]キーが押下されたとき、GcMask.Overwriteプロパティを使用して編集モードを取得します。
Imports GrapeCity.Win.CalendarGrid
Imports InputManCell = GrapeCity.Win.CalendarGrid.InputMan
Imports CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim gcMaskCellType As New InputManCell.CalendarGcMaskCellType()
gcMaskCellType.EditMode = InputManCell.EditMode.Insert
Dim template As New CalendarTemplate()
template.RowCount = 2
template.ColumnHeader.Rows(0).Cells(0).DateFormat = "{DayOfWeek}"
template.ColumnHeader.Columns(0).Width = 50
template.Content.Rows(0).Cells(0).DateFormat = "{MonthDay}"
template.Content.Rows(0).Cells(0).CellStyleName = "defaultStyle"
template.Content.Rows(1).Cells(0).Name = "myCell1"
template.Content.Rows(1).Cells(0).CellType = gcMaskCellType.Clone()
template.Content.Rows(1).Cells(0).CellStyleName = "defaultStyle"
GcCalendarGrid1.Template = template
End Sub
Private Sub GcCalendarGrid1_EditingControlShowing(sender As Object, e As CalendarEditingControlShowingEventArgs) _
Handles GcCalendarGrid1.EditingControlShowing
If TypeOf e.Control Is CalendarGridInputMan.GcMask Then
' CalendarGcMaskCellTypeの編集用コントロールが表示された場合にKeyDownイベントを設定します。
RemoveHandler e.Control.KeyDown, AddressOf Editor_KeyDown
AddHandler e.Control.KeyDown, AddressOf Editor_KeyDown
End If
End Sub
Private Sub Editor_KeyDown(sender As Object, e As KeyEventArgs)
Dim editor As CalendarGridInputMan.GcMask = DirectCast(sender, CalendarGridInputMan.GcMask)
If e.Alt AndAlso e.KeyCode = Keys.C Then
If editor.OverWrite = True Then
Console.WriteLine("上書き")
Else
Console.WriteLine("挿入")
End If
End If
End Sub
using GrapeCity.Win.CalendarGrid;
using InputManCell = GrapeCity.Win.CalendarGrid.InputMan;
using CalendarGridInputMan = GrapeCity.Win.CalendarGrid.Editors;
private void Form1_Load(object sender, EventArgs e)
{
var gcMaskCellType = new InputManCell.CalendarGcMaskCellType();
gcMaskCellType.EditMode = InputManCell.EditMode.Insert;
var template = new CalendarTemplate();
template.RowCount = 2;
template.ColumnHeader.Rows[0].Cells[0].DateFormat = "{DayOfWeek}";
template.ColumnHeader.Columns[0].Width = 50;
template.Content.Rows[0].Cells[0].DateFormat = "{MonthDay}";
template.Content.Rows[0].Cells[0].CellStyleName = "defaultStyle";
template.Content.Rows[1].Cells[0].Name = "myCell1";
template.Content.Rows[1].Cells[0].CellType = gcMaskCellType.Clone();
template.Content.Rows[1].Cells[0].CellStyleName = "defaultStyle";
gcCalendarGrid1.Template = template;
gcCalendarGrid1.EditingControlShowing += gcCalendarGrid1_EditingControlShowing;
}
private void gcCalendarGrid1_EditingControlShowing(object sender, CalendarEditingControlShowingEventArgs e)
{
if (e.Control is CalendarGridInputMan.GcMask)
{
// CalendarGcMaskCellTypeの編集用コントロールが表示された場合にKeyDownイベントを設定します。
e.Control.KeyDown -= new KeyEventHandler(Editor_KeyDown);
e.Control.KeyDown += new KeyEventHandler(Editor_KeyDown);
}
}
private void Editor_KeyDown(object sender, KeyEventArgs e)
{
var editor = sender as CalendarGridInputMan.GcMask;
if (e.Alt && e.KeyCode == Keys.C)
{
if (editor.OverWrite == true)
{
Console.WriteLine("上書き");
}
else
{
Console.WriteLine("挿入");
}
}
}
イベントを使った文字列操作
セルに文字列を入力するとGcMask.TextChangingイベントが、GcMask.TextChangedイベントの前(入力された文字列がTextプロパティに渡される前)に発生します。このイベント内で入力文字列をチェックすれば、Textプロパティの値に影響を与えることなく、入力を制御できます。
コンテキストメニューの表示
CalendarGridの他のセル型の場合と同様です。CalendarGcMaskCellType.ContextMenuStripを使用します。
関連トピック